/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsos.samcomm.forumManagement.boundary.jsf;

import de.hsos.samcomm.forumManagement.entity.Entry;
import de.hsos.samcomm.common.MessageHandler;
import de.hsos.samcomm.forumManagement.controller.ForumDAO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Diese Bean wird benötigt, um Einträge innerhalb aller Foren zu suchen.
 *
 * @author Dennis
 */
@ManagedBean(name = "search")
@RequestScoped
public class SearchBean implements Serializable {
    @Inject
    private ForumDAO forumDAO;

    @ManagedProperty("#{homeBean}")
    private HomeBean homeBean;

    @NotNull
    @Size(min=1, message = "{searchresult.message.noResults}")
    private String searchTerm;
    private List<Entry> results;

    /**
     * Default Konstruktor.
     */
    public SearchBean() {}

    /**
     * Sucht alle Einträge zu den übergebenen Autor.
     *
     * @param actionEvent
     */
    public void findEntriesByAuthor(ActionEvent actionEvent) {
        try {
            results = forumDAO.findEntriesByAuthor(searchTerm);
            homeBean.setActEntries(results);
            searchTerm = null;
            printMessage(actionEvent);
        }
        catch (Exception ex) {
            MessageHandler.addExceptionFacesMessage(ex);
        }
    }

    /**
     * Sucht alle Einträge zu den übergebenen Titel.
     *
     * @param actionEvent
     */
    public void findEntriesByTitle(ActionEvent actionEvent) {
        try {
            results = forumDAO.findEntriesByTitle(searchTerm);
            homeBean.setActEntries(results);
            searchTerm = null;
            printMessage(actionEvent);
        }
        catch (Exception ex) {
            MessageHandler.addExceptionFacesMessage(ex);
        }
    }

    /**
     * Get-Methode für den Suchbegriff.
     *
     * @return der Suchbegriff
     */
    public String getSearchTerm() {
        return searchTerm;
    }

    /**
     * Set-Methode für den Suchbegriff.
     *
     * @param searchTerm
     */
    public void setSearchTerm(String searchTerm) {
        this.searchTerm = searchTerm;
    }

    /**
     * Get-Methode für die Resultate.
     *
     * @return Alle Resultate
     */
    public List<Entry> getResults() {
        return results;
    }

    /**
     * Set-Methode für alle Resultate.
     *
     * @param results
     */
    public void setResults(List<Entry> results) {
        this.results = results;
    }

    /**
     * Get-Methode für die HomeBean.
     *
     * @return die HomeBean
     */
    public HomeBean getHomeBean() {
        return homeBean;
    }

    /**
     * Set-Methode für die HomeBean.
     *
     * @param homeBean
     */
    public void setHomeBean(HomeBean homeBean) {
        this.homeBean = homeBean;
    }

    /**
     * Gibt die Erfolgs- bzw Misserfolgsmeldung an Webseite.
     *
     * @param actionEvent
     * @throws FileNotFoundException
     * @throws IOException
     */
    private void printMessage(ActionEvent actionEvent) throws FileNotFoundException, IOException {
        if (results != null && results.size() > 0) {
            MessageHandler.addContextFacesMessage(FacesMessage.SEVERITY_INFO, "searchResults.message.success", "");
        }
        else {
            MessageHandler.addContextFacesMessage(FacesMessage.SEVERITY_WARN, "searchResults.message.failed", "searchResults.message.failed.info");
        }
    }

    /**
     * Setzt alle Strings zurück
     */
    private void clear() {
        searchTerm = "";
        results = null;
    }
}
